args DepVars Datalevel

clear
set obs `=$wsize*2+1'
gen year_post_reg = _n- $wsize-1

qui su year_post_reg
local min_year = `r(min)'
local max_year = `r(max)'

global plotopt scheme(s2mono) xlabel(`min_year'(1)`max_year') ylabel(, angle(0)) ///
	graphregion(color(gs16) ilc(gs16)) plotregion(lc(gs0)) ///
	xtitle("Years since the regulation", size(vlarge)) legend(off) 

foreach x in coeff se ci_min ci_max {
	gen `x' = 0 if year_post_reg == 0
}

foreach depvar in `DepVars' {

	cap gen `depvar' = 0
	run _labels

	local varlab: variable label `depvar'
	estimates use "${direc_estimates}/`depvar'-dynamic"

	foreach n of numlist 1/`=$wsize' `=$wsize+2'/`=2*$wsize+1' {
		quietly {
			local coeff = _b[`=`n'-1'.year_post_reg]
			local se = _se[`=`n'-1'.year_post_reg]
			replace coeff = `coeff' if _n == `n'
			replace se = `se' if _n == `n'
			replace ci_min = `coeff' + invt(`e(df_r)',0.05)* `se' if _n == `n'
			replace ci_max = `coeff' + invt(`e(df_r)',0.95)* `se' if _n == `n'
		}
	}	

	format coeff* %13.2fc

	quietly {
		twoway rarea ci_min ci_max year_post_reg, astyle(ci) || /// 
			function y = 0, range(`min_year' `max_year') lpattern(solid) lcolor(gs8) || ///
			scatter coeff year_post_reg , xline(0, lpattern(dash)) connect(l) lcolor(black)  mstyle(p1) mcolor(none)   || ///
			scatter coeff year_post_reg if year_post_reg <= 0 , msymbol(X) mstyle(p1)  msize(large)  mcolor(black) mlabpos(6) || ///
			scatter coeff year_post_reg if year_post_reg > 0 , mstyle(p1) mcolor(black)   mlabpos(12) || ///
			, $plotopt title("`varlab'", size(vlarge)) saving("${direc_temp}/`depvar'.gph", replace)
	}

	graph export "${direc_output}/`depvar'-`Datalevel'-level-reg-dynamic.pdf", replace
		
	
}
